<?php

namespace app\api\controller\kuerp\third;

use app\common\controller\Api;
use think\Db;
use think\Loader;
class Thirds extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $third_account;

    public function _initialize()
    {
        parent::_initialize();
        $data = input();
        $controllername = Loader::parseName($this->request->controller());
        $actionname = strtolower($this->request->action());
        //验证账户是否可以以及对应秘钥
        $account = isset($data['account'])?$data['account']:'';
        $requestSign = isset($data['sign'])?$data['sign']:'';
        $request_time = isset($data['request_time'])?$data['request_time']:0;
        if($request_time<(time()-300)){
            $this->error('无效请求','',23);
        }
        if(empty($account)){
            $this->error('account参数错误','',25);
        }
        if(empty($requestSign)){
            $this->error('签名错误','',24);
        }
        $third_account = Db::name('kuerp_third_account')->where(['account'=>$account])->find();
        $this->third_account = $third_account;
        if(empty($third_account)){
            $this->error('账户不存在','',21);
        }elseif($third_account['status']!=1){
            $this->error('账户状态异常','',22);
        }
        $salt = $third_account['salt'];

        //校验签名数组
        $rule = [
            'goods_goodstypelist' => 'account',//商品分类
            'goods_getgoodslist' => 'account',//商品列表
            'goods_getgoodsinfo' => 'account,id',//商品详情

            'customers_getsaleslist' => 'account',//职员列表
            'customers_getcustomerslist' => 'account',//客户列表
            'customers_joincustomers' => 'account,name,mobile',//添加客户
            'customers_customersinfo' => 'account,mobile',//客户详情
            'customers_editcustomers' => 'account,code,name,mobile',//更新客户
            'customers_delcustomers' => 'account,mobile',//注销客户

            'sales_getsaleslist' => 'account',//销货单列表
            'sales_sales' => 'account,order_num,mobile,goods',//添加销货单
            'sales_salesinfo' => 'account,purchase_code',//销货单详情
            'sales_returnsales' => 'account,order_num,mobile,goods',//添加销货退货单
            'sales_returnsalesinfo' => 'account,purchase_code',//销货退货单详情
        ];
        $controller_arr = explode('.',$controllername);
        $rule_name = $controller_arr[sizeof($controller_arr)-1].'_'.$actionname;
        if(!isset($rule[$rule_name])){
            $this->error('接口地址错误','',20);
        }

        //签名数组
        $sign_arr = [];
        $sign_key = explode(',',$rule[$rule_name]);
        foreach ($sign_key as $sign_key_k=>$sign_key_v){
            if(!isset($data[$sign_key_v])){
                $this->error($sign_key_v.'参数错误','',25);
            }
            $sign_arr[strtolower($sign_key_v)] = trim($data[$sign_key_v]);
        }
        ksort($sign_arr);
        foreach ($sign_arr as $sign_arr_k=>$sign_arr_v){
            $sign_arr[$sign_arr_k] = $sign_arr_k.'='.$sign_arr_v;
        }
        $sign_str = implode('&',$sign_arr).'key='.$salt;
        $sign = md5($sign_str);
        if($sign!=$requestSign){
            $this->error('签名错误','',24);
        }
    }

    function checkParameter($data,$parameter){
        if(is_array($parameter)){
            foreach ($parameter as $parameter_k=>$parameter_v){
                if(strpos($parameter_k,'|') !== false){
                    $k = explode('|',$parameter_k);
                    $name = $k[1];
                    $variable = $k[0];
                }else{
                    $name = $parameter_k;
                    $variable = $parameter_k;
                }
                $parameter_v = empty($parameter_v)?'require':$parameter_v;
                $rule = explode('|',$parameter_v);
                foreach ($rule as $rule_k=>$rule_v){
                    if($rule_v=='requireNone'){
                        if(!isset($data[$variable])){
                            return ['status'=>25,'msg'=>'请输入'.$name.'参数'];
                        }
                    }else if($rule_v=='require'){
                        if(!isset($data[$variable]) || $data[$variable]===''){
                            return ['status'=>25,'msg'=>$name.'参数不正确'];
                        }
                    }else{
                        $rule_row = explode(':',$rule_v);
                        switch ($rule_row[0]){
                            case 'in':
                                if(isset($data[$variable]) && $data[$variable]!==''){
                                    if(!isset($rule_row[1])){
                                        return ['status'=>26,'msg'=>'校验规则错误'];
                                    }
                                    $row_box = explode(',',$rule_row[1]);
                                    if(!in_array($data[$variable],$row_box)){
                                        return ['status'=>25,'msg'=>$name.'参数不正确'];
                                    }
                                }
                                break;
                            case 'between':
                                if(isset($data[$variable]) && $data[$variable]!==''){
                                    if(!isset($rule_row[1])){
                                        return ['status'=>26,'msg'=>'校验规则错误'];
                                    }
                                    $row_box = explode(',',$rule_row[1]);
                                    if($data[$variable]<$row_box[0] || $data[$variable]>$row_box[1]){
                                        return ['status'=>25,'msg'=>$name.'参数不正确'];
                                    }
                                }
                                break;
                            case 'number':
                                if(isset($data[$variable]) && $data[$variable]!==''){
                                    if (!preg_match("/^\d*$/", $data[$variable])){
                                        return ['status'=>25,'msg'=>$name.'只能为数字'];
                                    }
                                }
                                break;
                            case 'letter':
                                if(isset($data[$variable]) && $data[$variable]!==''){
                                    if (!preg_match("/^[a-zA-Z]+$/u", $data[$variable])){
                                        return ['status'=>25,'msg'=>$name.'只能为字母'];
                                    }
                                }
                                break;
                            case 'money':
                                if(isset($data[$variable]) && $data[$variable]!==''){
                                    if (!preg_match("/^[0-9]+(.[0-9]{1,4})?$/", $data[$variable])){
                                        return ['status'=>25,'msg'=>$name.'格式错误'];
                                    }
                                }
                                break;
                            case 'length':
                                if(isset($data[$variable]) && $data[$variable]!==''){
                                    if(!isset($rule_row[1])){
                                        return ['status'=>26,'msg'=>'校验规则错误'];
                                    }
                                    $row_box = explode(',',$rule_row[1]);
                                    if(strlen($data[$variable])<$row_box[0] || strlen($data[$variable])>$row_box[1]){
                                        return ['status'=>25,'msg'=>$name.'字符长度为'.$row_box[0].'-'.$row_box[1].'位'];
                                    }
                                }
                                break;
                            default:
                                return ['status'=>26,'msg'=>'校验规则错误'];
                        }
                    }
                }
            }
        }else{
            $parameter = explode(',',$parameter);
            foreach ($parameter as $k=>$v){
                if(!isset($data[$v]) || $data[$v]===''){
                    return ['status'=>25,'msg'=>'参数不正确'];
                }
            }
        }
        return true;
    }

    /**
     * 获取编号
     * @param int $type   编号类型
     * @return mixed
     */
    function getCodes($code,$type)
    {
        $set = \think\Db::name('kuerp_prefix')->where(['type'=>$type])->find();
        if(empty($set)){
            return '';
        }else{
            preg_match_all("/\d+/",$code,$num);
            $order_num = implode('',$num[0]);
            if($order_num<$set['start']){
                $order_num = $set['start'];
            }else{
                $order_num = (float)$order_num+1;
            }
            $prefix = $set['prefix'];
            if($set['year']==1){
                $prefix = $prefix.date('Y');
            }
            if($set['month']==1){
                $prefix = $prefix.date('m');
            }
            if($set['day']==1){
                $prefix = $prefix.date('d');
            }
            if($set['hour']==1){
                $prefix = $prefix.date('H');
            }
            if($set['minute']==1){
                $prefix = $prefix.date('i');
            }
            if($set['second']==1){
                $prefix = $prefix.date('s');
            }
            $s = '';
            if(strlen($order_num)<$set['strlen']){
                for ($a=1;$a<=$set['strlen']-(strlen($order_num));$a++){
                    $s .= '0';
                }
            }
            $code = $order_num;
            $order_num = $prefix.$s.$order_num;
        }
        if(!empty($set['dbname']) && !empty($set['field'])){
            $res = \think\Db::name($set['dbname'])->where([$set['field']=>$order_num])->value('id');
            if($res){
                return $this->getCodes($code,$type);
            }
        }
        return $order_num;
    }

}
